package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/21 14:35
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/getUsername")
    public Result getUsername() {
        //获取过滤器存储在session中的username
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null) {
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user);
    }

    @PostMapping("/getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = userService.getByConditions(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 获取全部的角色的信息
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll() {

        try {
            List<Role> roleList = userService.getAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds){
        //调用service传入参数
        try {
            userService.add(user, roleIds);
            return new Result(true,"添加用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户信息失败");
        }
    }

    @GetMapping("/get")
    public Result get(Integer id) {
        try {
            User user = userService.get(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 查询所有关联的中间表数据
     * @param id
     * @return
     */
    @GetMapping("/getRoleIdsById")
    public Result getCheckItemIdsById(Integer id) {

        try {
            List<Integer> roleIds = userService.getRoleIdsById(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 更新检查组，还有中间表的关系
     * @param user
     * @param roleIds
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user, Integer[] roleIds){
        //调用service传入参数
        try {
            userService.update(user, roleIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组，并且删除中间表信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
            return new Result(true,"删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }


}
